package com.xoms.front.service.impl;

import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xoms.front.aspect.datasource.DataSourceSwitcher;
import com.xoms.front.controller.res.bean.RProCategoryJSONBean;
import com.xoms.front.model.AccountBean;
import com.xoms.front.model.vo.ProCategoryVo;
import com.xoms.front.persist.dao.ProCategoryDao;
import com.xoms.front.service.ProCategoryService;
import com.xoms.util.StringUtil;

@Service
public class ProCategoryServiceImpl implements ProCategoryService {

	Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	ProCategoryDao proCategoryImpl;

	@Override
	public RProCategoryJSONBean getAllCategories(AccountBean account) {
		RProCategoryJSONBean bean = new RProCategoryJSONBean();
		JSONArray jsonArray = new JSONArray();
		// 从缓存获取所有分类
		String cateStr = null;// proCategoryCacheImpl.getAllCategoresCache();
		if (StringUtil.isEmpty(cateStr)) {// redis内分类信息为空则从数据库查询分类
			// 手动切换数据源
			DataSourceSwitcher.setDataSourceType(DataSourceSwitcher.DATA_SOURCE_HQ);
			List<ProCategoryVo> cates = proCategoryImpl.selectHqCates();
			// 手动切换数据源
			DataSourceSwitcher.setDataSourceType(DataSourceSwitcher.DATA_SOURCE_XOMS);
			List<ProCategoryVo> cate2s = proCategoryImpl.selectAllCates();
			if (cates == null) {
				return null;
			}
			if (cate2s == null) {
				return null;
			}

			int cate2Index = 0;
			for (ProCategoryVo vo : cates) {
				if (vo.getLevel() != null && vo.getLevel() == 2) {
					vo.setLevelIndex(cate2Index);
					cate2Index++;
				}
			}

			cates.addAll(cate2s);
			logger.info("cates分类：" + JSON.toJSONString(cates));
			// 构造根节点
			ProCategoryVo root = new ProCategoryVo();
			root.setCid("0");
			cates.add(root);
			// 根据父节点id分组

			Map<String, List<ProCategoryVo>> map = cates.stream().filter(o -> Objects.nonNull(o.getParentId()))
					.collect(Collectors.groupingBy(ProCategoryVo::getParentId));

			// 循环处理子节点 构建树状结构
			cates.forEach(proCategory -> {
				if (map.containsKey(proCategory.getCid())) {
					proCategory.setSubCategories(map.get(proCategory.getCid()));
				}
			});

			// 获取根节点下的所有服务项
			ProCategoryVo allCate = cates.stream().filter(proCategory -> "0".equals(proCategory.getCid())).findFirst()
					.orElse(null);
			List<ProCategoryVo> res = allCate.getSubCategories();
			
			// 遍历删除
			ListIterator<ProCategoryVo> iterator = res.listIterator();
//			while (iterator.hasNext()) {
//				ProCategoryVo pc = iterator.next();
//				List<ProCategoryVo> subCates = pc.getSubCategories();
//				ListIterator<ProCategoryVo> subIter = subCates.listIterator();
//				if (subCates == null || subCates.size() == 0) {
//					iterator.remove();
//				} else {
//					while (subIter.hasNext()) {
//						ProCategoryVo s = (ProCategoryVo) subIter.next();
//					}
//				}
//			}
			
			//TODO:判断删除
			String catesJson = JSON.toJSONString(res);
			logger.info("cates分类：" + catesJson);
			jsonArray = JSON.parseArray(catesJson);
			// 将分类存入缓存
			// proCategoryCacheImpl.setAllCategoryCache(catesJson);
		} else {
			jsonArray = JSON.parseArray(cateStr);
		}
		bean.setCategory(jsonArray);
		return bean;
	}

}
